package entity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 聊天内容详情
 */

public class Statement {

    //文本类型
    public static final int STATEMENT_TYPE_TEXT = 0;
    //图片
    public static final int STATEMENT_TYPE_IMAGE = 1;
    //语音
    public static final int STATEMENT_TYPE_AUDIO = 2;
    //地理位置
    public static final int STATEMENT_TYPE_LOCATION = 3;

    public static final int STATEMENT_STATE_DONE = 0;  //所有聊天
    public static final int STATEMENT_STATE_PEND = 1;  //自己聊天，正在发送
    public static final int STATEMENT_STATE_NEW = 2;   //别人语音聊天，未读
    public static final int STATEMENT_STATE_FAIL = 3;  //自己聊天，发送失败

    private Long id;

    //时间
    private long time;

    private Long userId;              //关联用户Id，当用户为联系人时有效

    private String iconUri;           //关联用户Icon，当用户非联系人时有效

    private String contentStr;         //字符串形式的信息内容，涵盖文本、图像/声音地址或URI、经纬度

    private int contentType;           //内容类型

    private int state;                  //状态

    private long chatId;               //聊天ID

    private boolean isOwn;            //

       //仅在服务端使用
    private long taggedId;

    private boolean belongContactChat; //是否属于联系人聊天

    public Statement(Long id, long time, Long userId, String iconUri,
                     String contentStr, int contentType, int state, long chatId,
                     boolean isOwn) {
        this.id = id;
        this.time = time;
        this.userId = userId;
        this.iconUri = iconUri;
        this.contentStr = contentStr;
        this.contentType = contentType;
        this.state = state;
        this.chatId = chatId;
        this.isOwn = isOwn;
    }

    public Statement() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public long getTime() {
        return this.time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getIconUri() {
        return this.iconUri;
    }

    void setIconUri(String iconUri) {
        this.iconUri = iconUri;
    }

    public String getContentStr() {
        return this.contentStr;
    }

    void setContentStr(String contentStr) {
        this.contentStr = contentStr;
    }

    public int getContentType() {
        return this.contentType;
    }

    void setContentType(int contentType) {
        this.contentType = contentType;
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public long getChatId() {
        return this.chatId;
    }

    public void setChatId(long chatId) {
        this.chatId = chatId;
    }

    public boolean getIsOwn() {
        return this.isOwn;
    }

    void setIsOwn(boolean isOwn) {
        this.isOwn = isOwn;
    }

    public void setTaggedId(long id) {
        this.taggedId = id;
    }

    public long getTaggedId() {
        return taggedId;
    }

    public void setBelongContactChat(boolean belongContactChat) {
        this.belongContactChat = belongContactChat;
    }

    public boolean belongContactChat() {
        return belongContactChat;
    }

    public void setLocation(double lng, double lat, String locStr) {
        this.contentStr = String.format(Locale.CHINA, "%f,%f", lng, lat) + "," + locStr;
    }

    public float getLng() {
        String[] strs = this.contentStr.split(",");
        return Float.parseFloat(strs[0]);
    }

    public float getLat() {
        String[] strs = this.contentStr.split(",");
        return Float.parseFloat(strs[1]);
    }

    public String getLocStr() {
        String[] strs = this.contentStr.split(",");
        return strs[2];
    }

    public void setAudioParams(int seconds, String path) {
        this.contentStr = String.format(Locale.CHINA, "%d", seconds) + ";" + path;
    }

    public String getAudioFilePath() {
        int pos = this.contentStr.indexOf(';');
        return this.contentStr.substring(pos + 1);
    }

    public int getAudioLength() {
        int pos = this.contentStr.indexOf(';');
        if (pos >= 1) {
            return Integer.parseInt(this.contentStr.substring(0, pos));
        }
        return 0;
    }

    public String getAudioLengthStr() {
        return String.format(Locale.CHINA, "%d", getAudioLength()) + "秒";
    }


    public String getTimeStr() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss aa", Locale.CHINA);
        return sdf.format(new Date(this.time));
    }

}
